package com.woniuxy.exam.controller;


import com.woniuxy.commons.entity.QuestionBank;
import com.woniuxy.commons.entity.ResponseEntity;
import com.woniuxy.exam.service.QuestionBankService;
import com.woniuxy.exam.utils.TreeUtils;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author zouyang
 * @date 2020/7/17 - 16:10
 */
@RestController
@RequestMapping("/qb")
public class QuestionBankController {

    @Autowired
    private QuestionBankService questionBankService;


    @RequestMapping("/all")
    public List<QuestionBank> getAllQuestionBank(){
        return questionBankService.selectAll();
    }

    @RequestMapping("/add")
    public ResponseEntity addQuestionBank(QuestionBank questionBank){
        if(questionBank.getPid()==null){
            questionBank.setPid(0);
        }
        int res = questionBankService.insert(questionBank);
        if (res==1){
            return ResponseEntity.SUCCESS_RESPONSE;
        }
        return new ResponseEntity(500,"出现异常");

    }

    @RequestMapping("/update")
    public ResponseEntity updateQuestionBank(QuestionBank questionBank){
        int res = questionBankService.updateByPrimaryKeySelective(questionBank);
        if (res==1){
            return ResponseEntity.SUCCESS_RESPONSE;
        }
        return new ResponseEntity(500,"出现异常");
    }

    @RequestMapping("/del")
    public ResponseEntity deleteQuestionBank(Integer QBId){
        int res = questionBankService.deleteByPrimaryKey(QBId);
        if (res==1){
            return ResponseEntity.SUCCESS_RESPONSE;
        }
        return new ResponseEntity(500,"出现异常");
    }

    @GetMapping("/menu")
    public List<QuestionBank> getMenu(){
        List<QuestionBank> all = questionBankService.selectAll();
        return TreeUtils.getMenu(all);
    }

    @GetMapping("/branches")
    public List<QuestionBank> getBranches(){
        List<QuestionBank> branches = questionBankService.selectAllPidGreaterThan0();
        return branches;
    }

    @PostMapping("/search")
    public List<QuestionBank> search(QuestionBank bank){
        List<QuestionBank> list = questionBankService.selectByAll(bank);
        return list;
    }

    @PostMapping("batchDel")
    public ResponseEntity batchDel(@RequestBody Integer[] ids){
        List<Integer> idList = new ArrayList<>(Arrays.asList(ids));
        int res = questionBankService.deleteByIdIn(idList);
        if(res>0){
            return ResponseEntity.SUCCESS_RESPONSE;
        }
        return new ResponseEntity(500,"出现异常");
    }
}
